package hash_map;

import java.util.*;

public class leetcode49_self {
    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> result = groupAnagrams(strs);
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i));
        }
    }

    public static List<List<String>> groupAnagrams(String[] strs){

        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            char[] chars = strs[i].toCharArray();
            Arrays.sort(chars);
            String l1 = new String(chars);
            if(!map.containsKey(l1)){
                map.put(l1,new ArrayList<>());
            }
            map.get(l1).add(strs[i]);
        }

        List<List<String>> result = new ArrayList<>(map.values());
        return result;
    }
}
